//
//  ForgotPasswordViewController.swift
//  ShiZhe
//
//  Created by 陈卿伟 on 16/8/31.
//  Copyright © 2016年 陈卿伟. All rights reserved.
//

import UIKit

class ForgotPasswordViewController: MainViewController {
    var forgotPasswordView:ForgotPasswordView!
    var vCodeString = "111111"//同手机注册时候的验证码  防止瞎写却搞对了
    var timeN = 60//读秒计数
    override func viewDidLoad() {
        super.viewDidLoad()
        self.title = "找回密码"
        self.addSMSView()
        self.addTargetToView()
    }
}

//MARK:--UI
extension ForgotPasswordViewController{
    //将view添加进来
    func addSMSView(){
        forgotPasswordView = ForgotPasswordView(frame: MainBounds)
        self.view.addSubview(forgotPasswordView)
    }
    
    //给控件添加点击事件
    func addTargetToView(){
        //登录按钮点击事件
        forgotPasswordView.logButton.addTarget(self, action: #selector(self.logButtonClick(_:)), forControlEvents: .TouchUpInside)
        
        //发送验证码按钮点击事件
        forgotPasswordView.provingButton.addTarget(self, action: #selector(self.proButtonClick(_:)), forControlEvents: .TouchUpInside)
    }
}

//MARK:--点击事件
extension ForgotPasswordViewController{
    //下一步按钮点击事件
    func logButtonClick(sender:UIButton){
        let phoneTF = forgotPasswordView.phoneNumber
        let vCodeTF = forgotPasswordView.vCodeTF
        if phoneTF?.text?.characters.count != 11{
            showHUD.showTopHud(self.view, text: "请输入正确的11位电话号码")
        }else{
            phoneTF?.resignFirstResponder()
            vCodeTF?.resignFirstResponder()
            if let hud = self.view.window!.viewWithTag(999) as? WaitHUD {
                hud.alpha = 1
                SMSSDK.commitVerificationCode(vCodeTF?.text, phoneNumber: phoneTF.text, zone: "86") { (error) in
                    if error == nil{
                        hud.alpha = 0
                        self.navigationController?.pushViewController(NewPasswordViewController(), animated: true)
                    }else{
                        hud.alpha = 0
                        let userInfo = error.userInfo["commitVerificationCode"] as! String
                        showHUD.showTopHud(self.view, text: userInfo)
                    }
                }
            }else{
                showHUD.showTopHud(self.view, text: "验证码错误")
            }
            
        }
    }
    
    
    //发送验证码按钮点击事件
    func proButtonClick(sender:UIButton){
        let phoneTF = forgotPasswordView.phoneNumber
        if phoneTF?.text?.characters.count != 11{
            showHUD.showTopHud(self.view, text: "请输入正确的11位电话号码")
        }else{
            sender.userInteractionEnabled = false
            //发送验证码
            let hud = showHUD.showWaitHud(self.view)
            SMSSDK.getVerificationCodeByMethod(SMSGetCodeMethodSMS, phoneNumber: phoneTF?.text, zone: "86", customIdentifier: nil) { (error) in
                hud.alpha = 0
                if error == nil{
                    NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: #selector(self.timeValueChanged(_:)), userInfo: sender, repeats: true)
                    self.timeN = 60
                }else{
                    let userInfo = error.userInfo["getVerificationCode"] as! String
                    showHUD.showTopHud(self.view, text: userInfo)
                    sender.userInteractionEnabled = true
                }
            }
        }
    }
    
    //点击发送验证码的时候 provingButton的时间转换事件
    func timeValueChanged(sender:NSTimer){
        let proButton = sender.userInfo as! UIButton
        if timeN == 60{
            proButton.backgroundColor = UIColor(red: 192/255, green: 192/255, blue: 192/255, alpha: 1)
        }
        proButton.setTitle("重新发送 (\(timeN)s)", forState: UIControlState())
        
        if timeN == 0{
            proButton.backgroundColor = UIColor.redColor()
            proButton.setTitle("发送验证码", forState: UIControlState())
            proButton.userInteractionEnabled = true
            sender.invalidate()
        }
        
        timeN -= 1
    }

}
